Strict

Import BRL.FreeAudioAudio
Import BRL.OggLoader

?Win32
Import "bass.bmx"
?

Global sfxvol# = 0.5
Global musicvol# = 0.5
?Mac
Global musicchannel:TChannel = Null
?Win32
Global musicchannel:Int = 0
?


Global sunloopchan:TChannel[]
sunloopchan = New TChannel[8]


' snds
Global nme_born_snd:TSound = LoadSound("samples/buzz3.ogg")			' 
Global nme1_born_snd:TSound = LoadSound("samples/pop2.ogg")			' 
Global nme2_born_snd:TSound = LoadSound("samples/pop3.ogg")			' 
Global nme3_born_snd:TSound = LoadSound("samples/snake1.ogg")			' 
Global nme4_born_snd:TSound = LoadSound("samples/gruntborn.ogg")			' 

Global nme5_born_snd:TSound = LoadSound("samples/sun1.ogg")			' 
Global nme5_loop_snd:TSound = LoadSound("samples/bondloop.ogg",True)			' 
Global nme5_shrink_snd:TSound = LoadSound("samples/sunhit1.ogg")			' 
Global nme5_grow_snd:TSound = LoadSound("samples/sizzle1.ogg")		' 
Global nme5_explode_snd:TSound = LoadSound("samples/sunexp.ogg")			' 
Global nme5_killed_snd:TSound = LoadSound("samples/Explo1.ogg")			' 

Global nme6_born_snd:TSound = LoadSound("samples/wee.ogg")
Global nme6_tailexplode_snd:TSound = LoadSound("samples/snakehit.ogg")
Global nme6_tailhit_snd:TSound = LoadSound("samples/tailhit.ogg")

Global nme7_born_snd:TSound = LoadSound("samples/warn1.ogg")			' 
Global nme7_shield_snd:TSound = LoadSound("samples/shield1.ogg")			' 

Global nme8_born_snd:TSound = LoadSound("samples/butterfly.ogg")			' 

Global ge_born_snd:TSound = LoadSound("samples/cat.ogg")			' 
Global ge_hit_snd:TSound = LoadSound("samples/genhit1.ogg")		
Global ge_killed_snd:TSound = LoadSound("samples/genkilled1.ogg")		'

Global le_born_snd:TSound = LoadSound("samples/buzz1.ogg")			' 
Global le_hit_snd:TSound = LoadSound("samples/echo1.ogg")			
Global le_killed_snd:TSound = LoadSound("samples/elastic.ogg")		'

Global pu_collect_snd:TSound = LoadSound("samples/bonus1.ogg")		' 

Global get_ready_snd:TSound = LoadSound("samples/startup.ogg")			' 

Global player_hit_snd:TSound = LoadSound("samples/die1.ogg")		' 

Global shot_born_snd:TSound = LoadSound("samples/shotborn.ogg")			' 
Global shot_hit_wall_snd:TSound = LoadSound("samples/shotwall.ogg")			' 

Global game_over_snd:TSound = LoadSound("samples/gameover.ogg")			' 

Global super_bomb_snd:TSound = LoadSound("samples/explo1.ogg")			' 

Global extra_life_snd:TSound = LoadSound("samples/brainborn.ogg")			' 

Global extra_bomb_snd:TSound = LoadSound("samples/buzz2.ogg")			' 

Global multiplier_increase_snd:TSound = LoadSound("samples/bonus2.ogg")		' 
Global bonus_born_snd:TSound = LoadSound("samples/bonusborn.ogg")		' 
Global high_score_snd:TSound = LoadSound("samples/bonus1.ogg")		' reused

Global quarkhitsound:TSound = LoadSound("samples/quarkhit.ogg")		' 
Global quarkhit2sound:TSound = LoadSound("samples/quarkhit2.ogg")		' 


Function PlaySound2:TChannel(snd:TSound, freq# = 1, pan#=0, vol# = 1)

	Local ch:TChannel = Null
	
	If sfxvol > 0
		ch=CueSound(snd)
		If freq <> 1
			SetChannelRate(ch,freq)
		EndIf
		SetChannelPan ch, pan		
		SetChannelVolume ch,sfxvol*vol
		ResumeChannel ch	
	EndIf
	Return ch

End Function


Function SetPanAndVolume(ch:TChannel, pan#, vol#)

	If ch <> Null
		If sfxvol > 0
			SetChannelPan ch, pan		
			SetChannelVolume ch,sfxvol*vol
		EndIf
	EndIf

End Function



Function AdjustVolume()
	Local t:Int
	
	For t = 0 To 7
		If sunloopchan[t] <> Null
			SetChannelVolume sunloopchan[t],sfxvol
		EndIf
	Next

End Function


Function StopLoopingSounds()
	Local t:Int
	
	For t = 0 To 7
		If sunloopchan[t] <> Null
			StopChannel(sunloopchan[t])
			sunloopchan[t] = Null
		EndIf
	Next

End Function


Function StartMusic(song:Int)
?MacOS
'need a way to play tunes on a mac
'ogg maybe?
Rem
	If musicchannel <> Null Then Return
	
	Local f$ = ""

	Select song
		Case 0 'intro
			f$ = "music/Theme0.ogg"
		Case 1 'in game
			f$ = "music/Theme1.ogg"	
		Case 2 'hi score
			f$ = "music/Theme2.ogg"
	End Select
		
	musicchannel = CueSound(LoadSound(f$))
	SetChannelVolume(musicchannel,musicvol)
	ResumeChannel(musicchannel)
EndRem

?Win32
	If musicchannel <> 0 Then Return
	
	Local f$ = ""

	Select song
		Case 0 'intro
			f$ = "music/Theme0.it"
		Case 1 'in game
			f$ = "music/Theme1.it"	
		Case 2 'hi score
			f$ = "music/Theme2.it"
	End Select
		
	If BASS_Free 
		BASS_Free()	
		Local dev:Int = BASS_Init(1,44100,0,0,0)
		If dev <> 0
			musicchannel = BASS_MusicLoad(False,f$,0,0,BASS_SAMPLE_LOOP Or BASS_MUSIC_RAMPS Or BASS_MUSIC_CALCLEN,0)
			BASS_ChannelSetAttributes(musicchannel,-1,100*musicvol,-1)
			BASS_ChannelPlay(musicchannel,False)
		EndIf
	EndIf
?
	
End Function


Function StopMusic()
?MacOS
'need a way to play tunes on a mac
'ogg maybe?
Rem
	If musicchannel <> Null
		StopChannel(musicchannel)
		musicchannel = 0
	EndIf
End Rem
?Win32
	If musicchannel <> 0
		BASS_ChannelStop(musicchannel)
		BASS_Free()
		musicchannel = 0
	EndIf
?	
End Function


Function SetMusicVolume()
?Mac
'need a way to play tunes on a mac
'ogg maybe?
Rem
	If musicchannel <> Null
		SetChannelVolume(musicchannel,musicvol)
	EndIf
End Rem
?Win32
	If musicchannel <>0
		BASS_ChannelSetAttributes(musicchannel,-1,100*musicvol,-1)
	EndIf
?

End Function